package com.glodon.paas.qto.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import com.glodon.paas.job.model.TaskStatus;

@Document(collection="qtoTask")
@Entity
@Table(name = "qto_task")
public class Task implements Serializable {
    private static final long serialVersionUID = 1L;
    private static String VIRTUAL_PREFIX = "virtual-"; // 虚拟任务的前缀

    @Id
    @javax.persistence.Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    private String id;
    @Indexed
    private String jobId;// 请求ID，用于跟踪一次请求的多个job执行
    private String parentId;// 父任务ID，当任务为并行分支任务时，创建一个虚拟父任务来管理这些分支子任务
    private String jobServerTaskId;// 来自job-server的Id
    private String function;// 任务名称
    @Indexed
    private Date startTime;// 调度请求转发到job-server时间
    private Date responseTime;// 接收到job-server返回状态的时间
    @Column(length=512)
    private String input; // 输入model
    @Column(length=1024)
    private String output; // 来至job-server的返回model
    private String stories; // 楼层
    private Integer progress = 0;// 任务执行进度
    private String message;
    private boolean virtualTask;// 是否为虚拟任务，如果一个任务要拆分成多个子任务，就为这个子任务生成一个虚拟的父任务，用于跟踪这些子任务.
    private int storyCount;// 当为虚拟父任务时，记录拆分的子任务数
    @Enumerated(EnumType.STRING)
    private TaskStatus status;
    public Task() {
    }
    public Task(String function, String jobId, String parentId, String model, String stories, boolean virtualTask,
            int storyCount) {
        if (virtualTask == true) {
            this.function = VIRTUAL_PREFIX + function;
            this.storyCount = storyCount;
        } else {
            this.function = function;
            this.storyCount = 0;
        }
        this.jobId = jobId;
        this.parentId = parentId;
        this.input = model;
        this.stories = stories;
        this.virtualTask = virtualTask;
        this.storyCount = storyCount;
        this.startTime = new Date();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String requestId) {
        this.jobId = requestId;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getJobServerTaskId() {
        return jobServerTaskId;
    }

    public void setJobServerTaskId(String backgroundId) {
        this.jobServerTaskId = backgroundId;
    }

    public String getFunction() {
        return function;
    }

    public void setFunction(String function) {
        this.function = function;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(Date finishTime) {
        this.responseTime = finishTime;
    }

    public String getInput() {
        return input;
    }

    public void setInput(String model) {
        this.input = model;
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String responseModel) {
        this.output = responseModel;
    }

    public String getStories() {
        return stories;
    }
    
    public void setStories(String stories) {
        this.stories = stories;
    }
    
    public Integer getProgress() {
        return progress;
    }

    public void setProgress(Integer progress) {
        this.progress = progress;
    }

    public boolean isVirtualTask() {
        return virtualTask;
    }

    public void setVirtualTask(boolean virtualJob) {
        this.virtualTask = virtualJob;
    }

    public int getStoryCount() {
        return storyCount;
    }

    public void setStoryCount(int children) {
        this.storyCount = children;
    }

    public TaskStatus getStatus() {
        return status;
    }
    
    public void setStatus(TaskStatus status) {
        this.status = status;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "Task [id=" + id + ", jobId=" + jobId + ", parentId=" + parentId + ", jobServerTaskId="
                + jobServerTaskId + ", function=" + function + ", startTime=" + startTime + ", responseTime="
                + responseTime + ", input=" + input + ", output=" + output + ", stories=" + stories + ", progress="
                + progress + ", virtualTask=" + virtualTask + ", storyCount=" + storyCount + ", status="
                + status + "]";
    }
}
